package snu.bi.MSAgent;

import java.util.ArrayList;
import java.util.TimeZone;

import snu.bi.MSAgent.DB.MSADBHandler;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MIActivity extends Activity implements OnDismissListener { //Menu Input Activity -> MIActivity
	/*
	1. �Ʒ��� ���� �˻��� �ϰ� �Ǹ� �ܾ��߿� cre �� ���Ե� �ܾ �˻��ϰ� �˴ϴ�.
	Cursor tCursor = db.query(TABLE_NAME, FROM ,DBSTRING + " like '%cre%'" , null, null, null, null);
	*/
	Button ok, cancel, remove;
	ListView lv;
	TextView cal_value, total_menu;
	//String dbpath;
	int flag;
	double total_cal;
	ArrayList<String> items;
	menuInputAdapter miadapter;
	ArrayList<String> selected_menu;
	ArrayList<Integer> selected_menu_id;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mi_lay);		
		
		//dbpath = getIntent().getStringExtra("dpath");		
		ok = (Button)findViewById(R.id.mi_ok_button);
		cancel = (Button)findViewById(R.id.mi_cancel_button);
		remove = (Button)findViewById(R.id.mi_remove_button);
		lv = (ListView)findViewById(R.id.mi_menu_list);
		cal_value = (TextView)findViewById(R.id.mi_menu_cal_value);
		total_menu = (TextView)findViewById(R.id.mi_menu_totalmenu);
		total_menu.setVerticalScrollBarEnabled(true);total_menu.setMovementMethod(new ScrollingMovementMethod());
		
		items = new ArrayList<String>();
		flag = 0;		
		total_cal = 0;
		selected_menu = new ArrayList<String>();
		selected_menu_id = new ArrayList<Integer>();
		
		//items.add("Menu A");items.add("Menu b");		
		items = getlistitem(-1);
		flag  = 1;
		
		miadapter = new menuInputAdapter(this, R.layout.menuinput_row, items);
		
		lv.setAdapter(miadapter);
		lv.setOnItemClickListener( new AdapterView.OnItemClickListener(){			
			public void onItemClick(AdapterView parent, final android.view.View v, final int position, long id) {				
				if(flag!=4)	{
					listpaint(position);					
				}
				else {
					selection(position);
					cal_value.setText(" "+Double.toString(Math.round(total_cal*100)/100)+" kcal");
				}
				flag++;
			}			
		});
		
	}
	public void button_click(View view) {		
		if(view == ok) {
			TimeZone timezone = TimeZone.getTimeZone("Etc/GMT-9");
			TimeZone.setDefault(timezone);
			
			MSADBHandler thisdb = MSADBHandler.open(this.getApplicationContext());			
			
			//newValues.put("calories", total_cal);
			String time = Long.toString(new java.util.Date().getTime());
			for(int i=0; i<selected_menu_id.size(); i++) {
				ContentValues newValues = new ContentValues();
				newValues.put("time", time);
				newValues.put("menu", selected_menu_id.get(i));
				thisdb.insert("menu_log", null, newValues);
			}			
			thisdb.close();
			
			finish();
		}
		else if(view == cancel) {
			finish();
		}
		else if(view == remove) {
			if(selected_menu.size()>0) {
				int last_id;
				selected_menu.remove(selected_menu.size()-1);
				last_id = selected_menu_id.get(selected_menu_id.size()-1);
				selected_menu_id.remove(selected_menu_id.size()-1);
				
				total_menu.setText(makeallmenu());
				//total_menu.setText("haha");
				
				MSADBHandler thisdb = MSADBHandler.open(this.getApplicationContext());
				Cursor c = thisdb.rawQuery("select calories from menu where _id = "+last_id);
				c.moveToFirst();				
				total_cal = total_cal - c.getDouble(0);		
				
				c.close();
				thisdb.close();
				
				cal_value.setText(" "+Double.toString(Math.round(total_cal*100)/100)+" kcal");				
			}
		}
	}
	public void listpaint(int position) {
		//Toast.makeText(this, Integer.toString(position), Toast.LENGTH_SHORT).show();		
		items = getlistitem(position);
		listrepaint();
	}
	public ArrayList<String> getlistitem(int position) {
		ArrayList<String> ll = new ArrayList<String>();
		MSADBHandler thisdb = MSADBHandler.open(this.getApplicationContext());
		Cursor c;
		
		if(flag == 0) c = thisdb.rawQuery("select category_1 from menu group by category_1");
		else if(flag == 1) c = thisdb.rawQuery("select category_2 from menu where category_1 = '"+items.get(position)+"' group by category_2");
		else if(flag == 2) c = thisdb.rawQuery("select category_3 from menu where category_2 = '"+items.get(position)+"' group by category_3");
		else c = thisdb.rawQuery("select name from menu where category_3 = '"+items.get(position)+"'");		
		
		c.moveToFirst();
		ll.add(c.getString(0));
		for(int i=0; i<c.getCount()-1; i++) {
			c.moveToNext();
			ll.add(c.getString(0));						
			//ll.add("haha");
		}
		c.close();
		
		thisdb.close();
		return ll;
	}
	public ArrayList<String> Search(String word) {
		ArrayList<String> ll = new ArrayList<String>();
		MSADBHandler thisdb = MSADBHandler.open(this.getApplicationContext());
		
		Cursor c = thisdb.rawQuery("select name from menu where name like '%"+word+"%'");
		
		if(c.getCount() == 0) return ll;
		
		c.moveToFirst();
		ll.add(c.getString(0));
		for(int i=0; i<c.getCount()-1; i++) {
			c.moveToNext();
			ll.add(c.getString(0));						
			//ll.add("haha");
		}
		
		c.close();
		thisdb.close();
		
		return ll;
	}
	public void listrepaint() {
		miadapter = new menuInputAdapter(this, R.layout.menuinput_row, items);		
		lv.setAdapter(miadapter);
		lv.refreshDrawableState();
	}
	public void selection(int position) {
		if(selected_menu_id.size()<10) {
			MSADBHandler thisdb = MSADBHandler.open(this.getApplicationContext());
			Cursor c = thisdb.rawQuery("select _id, unit, calories from menu where name = '"+items.get(position)+"'");
			c.moveToFirst();
			selected_menu.add(items.get(position));
			selected_menu_id.add(c.getInt(0));
			//String temp = c.getString(1);
			total_cal += c.getDouble(2);
			
			c.close();
			thisdb.close();
			
			total_menu.setText(makeallmenu());
			
			flag = 0;
			items = getlistitem(-1);
			listrepaint();
		}
		else {
			Toast.makeText(this, "You can input max 10 menus at once", Toast.LENGTH_SHORT).show();
			flag = 3;
		}
	}
	public String makeallmenu() {
		if(selected_menu.size() == 0) {
			String re = new String();
			re = "";
			return re;
		}
		else {
			String re = selected_menu.get(0);
			for(int i=1; i<selected_menu.size(); i++) {
				re = re + ", " +selected_menu.get(i);
			}
			return re;
		}
	}
	public void search_click(View view) {
		valuepopup vp = new valuepopup(this, -1);
		vp.setOnDismissListener(this);
		vp.show();
	}
	public void onDismiss(DialogInterface dialog) {
		// TODO Auto-generated method stub
		valuepopup temp_vp = (valuepopup) dialog;
		String word = temp_vp.iv.getText().toString();
		
		if(word.equals("")) {
			flag = 0;
			items = getlistitem(-1);
			listrepaint();
			return;
		}
		else {
			flag = 4;
			items = Search(word);
			listrepaint();
			return;
		}
	}
}
